//
//  ZMProductCell.swift
//  LoveBeen
//
//  Created by pg on 2017/8/9.
//  Copyright © 2017年 pg. All rights reserved.
//

import UIKit

class ZMProductCell: UICollectionViewCell {

    var productModel: ZMProductsModel?{
        didSet{
            self.iconView.sd_setImage(with: URL(string:productModel?.img ?? ""), placeholderImage: #imageLiteral(resourceName: "v2_placeholder_square"))
            self.titleLabel.text = productModel?.name
            self.priceLabel.text = productModel?.price
        }
    }
    //MARK:- 懒加载图片显示view
    fileprivate lazy var iconView: UIImageView = {
       
        let imageview = UIImageView()
        return imageview
    }()
    //MARK:- 添加标题view
    fileprivate lazy var titleLabel: UILabel = {
       
        let title = UILabel(text: "", fontSize: 15, textColor: UIColor.darkGray)
        title.sizeToFit()
        title.numberOfLines = 2
        return title
    }()
    //MARK:- 懒加载价格视图
    fileprivate lazy var priceLabel: UILabel = {
       
        let label = UILabel(text: "", fontSize: 13, textColor: UIColor.red)
        label.sizeToFit()
        return label
    }()
    //MARK:- 懒加载添加购物车视图  + - 按钮
    fileprivate lazy var addproductToShopCarView: ZMAddToShoppingCarView = {
       
        let addview = ZMAddToShoppingCarView()
        
        return addview
    }()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.white
        initUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func initUI(){
        //添加iconview
        contentView.addSubview(iconView)
        iconView.snp.makeConstraints { (make) in
            make.top.leading.trailing.equalTo(contentView)
            make.height.equalTo(220)
        }
        
        //添加标题view
        contentView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.top.equalTo(iconView.snp.bottom).offset(5)
            make.centerX.equalTo(iconView)
        }
        //添加价格视图
        contentView.addSubview(priceLabel)
        priceLabel.snp.makeConstraints { (make) in
            make.top.equalTo(titleLabel.snp.bottom).offset(5)
            make.leading.equalTo(iconView).offset(10)
            make.bottom.equalTo(contentView).offset(-5)
        }
        //添加 + - 按钮视图
        contentView.addSubview(addproductToShopCarView)
        addproductToShopCarView.snp.makeConstraints { (make) in
            make.trailing.equalTo(contentView).offset(-5)
            make.bottom.equalTo(contentView).offset(-5)
        }
        
        //添加商品到购物车按钮点击回调 代码块
        addproductToShopCarView.addToShoppintCarViewButtonClickClosure = {[unowned self] (isAddButton) in
            if isAddButton {
                //点击添加按钮
                self.productModel?.buyNum += 1
                //获取当前cell的imageview的中心点坐标
                let iconCenter: CGPoint =  self.iconView.center
                //将坐标转到window上
                let covernPoint = self.contentView.convert(iconCenter, to: keywindow)
                //发送添加通知
                NotificationCenter.default.post(name: NSNotification.Name(AddToShoppingCarViewNotification), object: nil, userInfo: ["model": self.productModel!,"point": covernPoint,"image": self.iconView.image!])
                
                
            }else
            {
                //点击减少按钮
                self.productModel?.buyNum -= 1
                
                //发送添加通知
                NotificationCenter.default.post(name: NSNotification.Name(SubFromShoppingCarViewNotification), object: nil, userInfo: ["model": self.productModel! ])
            }
            self.addproductToShopCarView.count = self.productModel?.buyNum ?? 0
        }
        
    

        
        
    }

}
